package com.tianshi.order.controller;


import com.tianshi.entity.Users;
import com.tianshi.vo.CartVO;
import com.tianshi.order.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    private Long getLoginUserId(HttpSession session){
        Users user =(Users) session.getAttribute("loginUser");
        if(user==null){
            log.info("用户未登录");
            return null;
        }
        log.info("当前用户:{}",user.getUserId());
        return user.getUserId();
    }

    /*
    * 商品加入购车
    * */
    @PostMapping("/add/{productId}")
    public ResponseEntity<String> addCart(@PathVariable("productId") Long productId,HttpSession session){
        log.info("addCart:{}",productId);

        //获取当前用户的id
        Long userId = getLoginUserId(session);
        if(userId==null) return ResponseEntity.badRequest().body("用户未登录");
//        Long userId = 1L;
        Integer res = cartService.addCart(productId,userId);

        if(res>0) return ResponseEntity.ok("success");
        return ResponseEntity.badRequest().body("error");
    }

    /*
    * 根据用户id获取购物车列表
    * 流控测试
    * http://localhost/api/cart/list
    * */
    @GetMapping("/list") //
    public ResponseEntity<List<CartVO>> getCartList(HttpSession session){
        log.info("getCartList");

        //获取当前用户的id
        Long userId = getLoginUserId(session);
        if(userId==null) return ResponseEntity.badRequest().body(null);
//        Long userId = 1L;
        List<CartVO> cartList = cartService.getCartList(userId);
        return ResponseEntity.ok(cartList);
    }


    /*
    * 增加一个商品
    * */
    @PostMapping("/addOne/{productId}")
    public ResponseEntity<String> addOne(@PathVariable("productId") Long productId,HttpSession session){
        log.info("addOne:{}",productId);

        //获取当前用户的id
        Long userId = getLoginUserId(session);
        if(userId==null) return ResponseEntity.badRequest().body("用户未登录");
//        Long userId = 1L;
        Integer res = cartService.addOne(productId,userId);

        if(res>0) return ResponseEntity.ok("success");
        return ResponseEntity.badRequest().body("error");
    }


    /*
    * 减少一个物品
    * */
    @PostMapping("/subOne/{productId}")
    public ResponseEntity<String> subOne(@PathVariable("productId") Long productId,HttpSession session){
        log.info("subOne:{}",productId);

        //获取当前用户的id
        Long userId = getLoginUserId(session);
        if(userId==null) return ResponseEntity.badRequest().body("用户未登录");
//        Long userId = 1L;
        Integer res = cartService.subOne(productId,userId);

        if(res>0) return ResponseEntity.ok("success");
        return ResponseEntity.badRequest().body("error");
    }

    /*
    *
    * 删除购物车中的商品
    * */
    @DeleteMapping("/delete/{productId}")
    public ResponseEntity<String> deleteCart(@PathVariable("productId") Long productId,HttpSession session) {
        log.info("deleteCart:{}", productId);

        //获取当前用户的id
        Long userId = getLoginUserId(session);
        if(userId==null) return ResponseEntity.badRequest().body("用户未登录");
//        Long userId = 1L;
        Integer res = cartService.deleteCart(productId, userId);

        if (res > 0) return ResponseEntity.ok("success");
        return ResponseEntity.badRequest().body("error");
    }


    /*
    * 获取购物车的id
    * */
    @GetMapping("/getCartId")
    public ResponseEntity<Long> getCartId(HttpSession session) {
        log.info("getCartId");

        //获取当前用户的id
        Long userId = getLoginUserId(session);
        if(userId==null) return ResponseEntity.badRequest().body(null);
//        Long userId = 1L;
        Long cartId = cartService.getCartId(userId);

        return ResponseEntity.ok(cartId);
    }
}
